<template>
    <div class="login">
        <div class="box">
            <div class="logo-box">
                <img src="../assets/logo.png" alt="">
            </div>
            <el-form :rules="rules" ref="form" :model="form">
                <el-form-item label="账号" prop="username">
                    <el-input v-model="username" :prefix-icon="Avatar"></el-input>
                </el-form-item>
                 <el-form-item label="密码" prop="password">
                    <el-input v-model="password" :prefix-icon="View"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button @click="">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script setup>
    import { ref ,reactive,toRefs } from 'vue'
    import { useRouter } from 'vue-router'
    import { Login } from '../utils/login.js'
    import { ElMessage } from 'element-plus'
    import { useStore } from 'vuex'
    const store = useStore();
    const rules = {
        username:[
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 5, message: '用户名长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password:[
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 12, message: '密码长度在 6 到 12 个字符', trigger: 'blur' }   
        ]
    }
    const form = reactive({
        username:'admin',
        password:'123456'
    })
    const router = useRouter();
    let times = ref('');
    const { username,password } = toRefs(form);
    const login =  () => {
        clearTimeout(times.value);
        times.value = setTimeout(() => {
                if(username.value == '' || password.value == ''){
                    return  ElMessage.warning('用户名和密码不能为空');
                }//汪宇涵是你爹,你看我行不
                //返回值
        Login(form).then((data)=>{
                console.log(data,51)
                if(data.meta.status == 200){
                    ElMessage.success(data.meta.msg);
                    store.commit('setUser',data.data);
                    router.push('/index');
                }
            })
        }, 300);
        
        // console.log(res.data);
    }
</script>
<style lang="scss" scoped>
    .login{
        width: 100%;
        height: 100%;
        position: relative;
        background: #2b4b6b;
        .box{
            width: 500px;
            height: 330px;  
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            background: #fff;
            border-radius: 3px;
            box-shadow: 0 0 10px #ccc;
            padding: 130px 20px 20px 20px;
            .logo-box{
                width: 180px;
                height: 180px;
                border-radius: 50%;
                padding: 10px;
                box-shadow: 0 0 10px #ccc;
                position: absolute;
                top: -50%;
                left: 50%;
                transform: translate(-50%,50%);
                background: #fff;
                &>img{
                    width: 100%;
                    border-radius: 50%;
                    background: #eee;
                }
            }
        }
        .btns{
            padding-left: 150px;
        }
    }
</style>